package com.wangsh.userscenter.common.controller;

import java.io.IOException;
import java.io.PrintWriter;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;

import com.alibaba.fastjson.JSONObject;
import com.wangsh.userscenter.common.util.ConstatFinalUtil;
import com.wangsh.userscenter.common.util.DateUtil;
import com.wangsh.userscenter.common.util.FileUtil;
import com.wangsh.userscenter.common.util.PageInfoUtil;
import com.wangsh.userscenter.common.util.RegexUtil;
import com.wangsh.userscenter.common.util.SpringEmailUtil;
import com.wangsh.userscenter.common.util.VerifyCodeUtils;

/**
 * controller的超级父类
 * 
 * @author wangshh
 * 
 */
public class BaseController
{
	@Resource
	protected DateUtil dateUtil ;
	@Resource
	protected RegexUtil regexUtil ;
	@Resource
	protected FileUtil fileUtil ;
	
	@Autowired
	protected SpringEmailUtil springEmailUtil ; 
	
	/* 返回jsp存储信息 */
	protected String info = "未知"; 

	/**
	 * 获取真实IP
	 * 
	 * @return
	 */
	protected String findRealIpUtil(HttpServletRequest request)
	{
		String ip = request.getHeader("x-forwarded-for");
		if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip))
		{
			ip = request.getHeader("Proxy-Client-IP");
		}
		if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip))
		{
			ip = request.getHeader("WL-Proxy-Client-IP");
		}
		if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip))
		{
			ip = request.getHeader("x-real-ip");
		}
		if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip))
		{
			ip = request.getRemoteAddr();
		}
		// String ip = request.getParameter("ip");
		return ip;
	}

	/**
	 * 设置page的信息（默认的值）
	 * 
	 * @param pageinfo
	 * @param cond
	 * @return
	 */
	protected PageInfoUtil setPageInfoCond(HttpServletRequest request)
	{
		/* 接收分页参数 */
		String currentPageStr = request.getParameter("currentPage");
		String pageSizeStr = request.getParameter("pageSize");
		
		PageInfoUtil pageInfoUtil = new PageInfoUtil();
		int currentPage = 1;
		int pageSize = Integer.valueOf(ConstatFinalUtil.SYS_BUNDLE
				.getString("houtai.pageinfo.pagesize"));
		try
		{
			currentPage = Integer.valueOf(currentPageStr);
			pageSize = Integer.valueOf(pageSizeStr);
		} catch (NumberFormatException e)
		{

		}
		pageInfoUtil.setCurrentPage(currentPage);
		pageInfoUtil.setPageSize(pageSize);
		return pageInfoUtil;
	}
	
	/**
	 * 从session中获取用户或者管理员信息
	 * @return
	 */
	protected Object findObjfromSession(HttpServletRequest request , String type)
	{
		HttpSession session = request.getSession();
		if("1".equalsIgnoreCase(type))
		{
			return session.getAttribute("users");
		}else if("2".equalsIgnoreCase(type))
		{
			return session.getAttribute("admins");
		}
		return null ; 
	}
	
	/**
	 * 返回json字符串
	 * @param request
	 * @param info
	 * @return
	 */
	public JSONObject returnJSON(HttpServletRequest request , String info)
	{
		//处理返回的json
		String callbackType = request.getParameter("callbackType");
		String navTabId = request.getParameter("navTabId");
		String rel = request.getParameter("rel");
		String forwardUrl = request.getParameter("forwardUrl");
		
		if(callbackType == null)
		{
			callbackType = "" ; 
		}
		if(navTabId == null)
		{
			navTabId = "" ; 
		}
		if(rel == null)
		{
			rel = "" ; 
		}
		if(forwardUrl == null)
		{
			forwardUrl = "" ; 
		}
		
		if(request.getAttribute("callbackType") != null)
		{
			callbackType = request.getAttribute("callbackType") + "" ; 
		}
		
		JSONObject resultJSON = new JSONObject();
		resultJSON.put("message", info);
		resultJSON.put("confirmMsg", "");
		resultJSON.put("callbackType", callbackType);
		resultJSON.put("navTabId", navTabId);
		resultJSON.put("rel", rel);
		resultJSON.put("forwardUrl", forwardUrl);
		resultJSON.put("statusCode", "200");
		return resultJSON ; 
	}
	
	/**
	 * 返回json字符串
	 * @param request
	 * @param info
	 * @return
	 * @throws IOException 
	 */
	public JSONObject printOut(HttpServletRequest request , HttpServletResponse response , String returnStr)
	{
		try
		{
			response.setContentType("text/html;charset=UTF-8");
			PrintWriter out = response.getWriter();
			out.print(returnStr);
			out.flush();
			out.close();
		} catch (IOException e)
		{
			ConstatFinalUtil.SYS_LOG.error("为客户端返回信息出错了;" , e);
		}
		return null; 
	}
	
	/**
	 * 图片验证码
	 * @return
	 * @throws IOException 
	 */
	@RequestMapping("/imgCode.html")
	public String imgCode(HttpServletRequest request,HttpServletResponse response) throws IOException
	{
		response.setHeader("Pragma", "No-cache");  
		response.setHeader("Cache-Control", "no-cache");  
		response.setDateHeader("Expires", 0);  
		response.setContentType("image/jpeg");  
		  
		//生成随机字串  
		String verifyCode = VerifyCodeUtils.generateVerifyCode(4);  
		//存入会话session  
		HttpSession session = request.getSession(true);  
		session.setAttribute("randSess", verifyCode.toLowerCase());  
		//生成图片  
		int w = 100, h = 40;  
		VerifyCodeUtils.outputImage(w, h, response.getOutputStream(), verifyCode);
		return null ; 
	}
	
	
	/**
	 * 公共的页面
	 * @return
	 */
	@RequestMapping("/commonInfo.html")
	protected String commonInfo(HttpServletRequest request)
	{
		return "commonInfo";
	}
	
	/**
	 * 清空信息
	 * @param request
	 */
	public void clearInfo(HttpServletRequest request)
	{
		HttpSession session = request.getSession() ;
		this.info = "" ; 
		request.setAttribute("info", info);
		session.setAttribute("info", info);
	}
}
